package service

import "manager/model"

type CustomerService struct {
	customers   []model.Customer
	customerNum int
}

func NewCustomerService() *CustomerService {
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(1, "张三")
	customerService.customers = append(customerService.customers, customer)
	return customerService
}

func (this *CustomerService) List() []model.Customer {
	return this.customers
}

func (this *CustomerService) Add(customer model.Customer) bool {
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers, customer)
	return true
}

func (this *CustomerService) FindById(id int) int {
	for i := 0; i < len(this.customers); i++ {
		if this.customers[i].Id == id {
			return i
		}
	}
	return -1
}

func (this *CustomerService) Delete(id int) bool {
	idx := this.FindById(id)
	if -1 == idx {
		return false
	}
	this.customers = append(this.customers[:idx], this.customers[idx+1:]...)
	return true
}
